package com.example.ordersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ordersystem.entity.Food;
import com.example.ordersystem.mapper.FoodMapper;
import com.example.ordersystem.service.FoodService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Resource
    FoodMapper foodMapper;

    /**
     * 添加菜品
     * @param food 菜品对象
     */
    @Override
    public void add(Food food) {
        foodMapper.insert(food);
    }

    /**
     * 删除菜品
     */
    @Override
    public void del(String id) {
        foodMapper.deleteById(id);
    }

    /**
     * 修改菜品
     * @param food 菜品对象
     */
    @Override
    public void change(Food food) {
         foodMapper.updateById(food);
    }

    /**
     * 根据id查询菜品信息
     * @param id 菜品id
     * @return 菜品对象
     */
    @Override
    public Food selectById(String id) {
        System.out.println(foodMapper.selectList(null));
         return foodMapper.selectById(id);
    }

    /**
     * 分页获取菜品列表
     * @param currentPage 当前页数
     * @return 分页数据
     */
    @Override
    public Page<Food> selectByPage(int currentPage) {
        return foodMapper.selectPage(new Page<>(currentPage, 10), new QueryWrapper<>());

    }

    /**
     * 扣减菜品库存
     * @param id 菜品id
     * @param num 扣减数量
     */
    @Override
    public void reduceStock(String id, String num) {
        Food food = foodMapper.selectById(id);
        int newNum=Integer.parseInt(food.getStock())-Integer.parseInt(num);
        food.setStock(String.valueOf(newNum));
        foodMapper.updateById(food);
    }

    /**
     * 首页获取推荐菜品
     */
    @Override
    public List<Food> homeGet() {
       return foodMapper.homeGet();
    }

    /**
     * 根据名称查询菜品
     */
    @Override
    public List<Food> selectByName(String name) {
        QueryWrapper<Food> wrapper=new QueryWrapper<Food>()
                .like("name",name);
        return foodMapper.selectList(wrapper);
    }

    /**
     * 删除多个菜品
     */
    @Override
    public void delMore(List<Food> foodList) {
         for(int i=0;i<foodList.size();i++)
         {
             foodMapper.deleteById(foodList.get(i));
         }
    }
}
